package com.cph.es.controller;

import com.alibaba.fastjson.JSON;
import com.cph.common.http.HttpResponseResult;
import com.cph.common.util.DateUtils;
import com.cph.es.pojo.VehicleCar;
import com.cph.es.service.AddVehicleCarDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/vehicle_data")
@Slf4j
public class AddVehicleCarDataController {
    @Autowired
    private AddVehicleCarDataService addVehicleCarDataService;
    private static List<String> hphms = new ArrayList<>();
    private static List<String> sbbhs = new ArrayList<>();
    private static List<String> clpps = new ArrayList<>();
    private Random random = new Random();

    static {

        hphms.add("沪C34969");
        hphms.add("鄂CB4169");
        hphms.add("粤DB1169");
        hphms.add("鄂AB1161");
        hphms.add("粤C11111");
        hphms.add("沪A11111");
        hphms.add("粤A11111");
        hphms.add("粤B11111");
        hphms.add("粤H11111");
        hphms.add("豫A11123");

        sbbhs.add("31011416001320011015");
        sbbhs.add("31010420051321003002");
        sbbhs.add("31011413001320001017");
        sbbhs.add("31010900001210161102");
        sbbhs.add("31011524001321006007");
        sbbhs.add("31015101001320012003");
        sbbhs.add("31011422001320001018");
        sbbhs.add("31011618001198001041");
        sbbhs.add("31010517001321011008");
        sbbhs.add("31010515001321051073");

        clpps.add("大众");
        clpps.add("宝马");
        clpps.add("奔驰");
        clpps.add("奥迪");
        clpps.add("法拉利");
        clpps.add("保时捷");
        clpps.add("兰博基尼");
        clpps.add("荣威");
        clpps.add("马自达");
        clpps.add("现代");
    }

    @PostMapping("/create_vehicle_data")
    public HttpResponseResult createVehicleCarData() {
        VehicleCar vehicleCar = new VehicleCar();
        vehicleCar.setId(getId());
        vehicleCar.setAreainserttime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getNowDate()));
        vehicleCar.setCalling("1");
        vehicleCar.setCdbh("1");
        vehicleCar.setCdfx("1");
        vehicleCar.setCllx("X99");
        vehicleCar.setCdbh("123");
        String clpp = clpps.get(random.nextInt(10));
        vehicleCar.setClpp(clpp);
        vehicleCar.setClsd("0");
        vehicleCar.setClzpp("");
        vehicleCar.setCorrecttime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getNowDate()));
        vehicleCar.setCsys("99");
        vehicleCar.setDatasource("15.0.17.120:53560");
        vehicleCar.setDt("202003");
        String hphm = hphms.get(random.nextInt(10));
        vehicleCar.setHphm(hphm);
        vehicleCar.setHpsf(hphm.substring(0,1));
        vehicleCar.setHpys("5");
        vehicleCar.setHpzl("01");
        vehicleCar.setIdentifiynum("1");
        vehicleCar.setInsert_date(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.getNowDate()));
        vehicleCar.setInsert_proc_no("1");
        vehicleCar.setInsert_time(String.valueOf(System.currentTimeMillis()));
        vehicleCar.setIscheckdevice("1");
        String jlbh = "31011525001321006014022019112719170" + System.currentTimeMillis();
        vehicleCar.setJlbh(jlbh);
        vehicleCar.setnBreakRuleType("0");
        vehicleCar.setnSecSafetyBelt("0");
        vehicleCar.setnSecSunvisor("0");
        vehicleCar.setRelatedInfoId("");
        vehicleCar.setSafety_belt("0");
        vehicleCar.setJgsj(String.valueOf(System.currentTimeMillis()));
        String sbbh = sbbhs.get(random.nextInt(10));
        vehicleCar.setSbbh(sbbh);
        vehicleCar.setStrLocalReciveTime(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, DateUtils.getNowDate()));
        vehicleCar.setStrSubTranTime(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, DateUtils.getNowDate()));
        vehicleCar.setSun_visor("1");
        vehicleCar.setTztp("http://15.16.75.33:9444/44248,2d495902c6dfb69e");
        vehicleCar.setXlinserttime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getNowDate()));
        vehicleCar.setXzqh("310115");
        log.info("vehicleCar value is = {}", JSON.toJSONString(vehicleCar));
        addVehicleCarDataService.saveVehicleCarData(vehicleCar);
        return HttpResponseResult.success("新增成功");
    }

    private static long tmpID = 0;

    private static boolean tmpIDlocked = false;

    public static long getId()
    {
        long ltime = 0;
        while (true)
        {
            if(tmpIDlocked == false)
            {
                tmpIDlocked = true;
                //当前：（年、月、日、时、分、秒、毫秒）*10000
                ltime = Long.valueOf(new SimpleDateFormat("yyMMddhhmmssSSS").format(new Date()).toString()) * 10000;
                if(tmpID < ltime)
                {
                    tmpID = ltime;
                }
                else
                {
                    tmpID = tmpID + 1;
                    ltime = tmpID;
                }
                tmpIDlocked = false;
                return ltime;
            }
        }
    }
}
